﻿"use strict";
define(['admin/app', "service/admin/merchIntroduceService", "angular-kindeditor", "ui.file.manage"], function (app) {
    app.controller('merchIntroduceController', function ($scope, $stateParams, $location, $uibModal, $state, utils, merchIntroduceService) {
        $scope.query = {
            page: 1, size: 20, q: '', title:''
        };
        $scope.editConfig = {
            autoHeightMode: true,
            items: ['source', 'preview', '|', 'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline', 'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist', 'insertunorderedlist']
        };
        $.extend($scope.query, $stateParams);
        var methods = {
            textAreaChanged: function (wordCount) {
                $scope.textcount = wordCount - $scope.model.detail.length;
                if ($scope.textcount <= 0) {
                    $scope.textcount = 0;
                    $("#textcount").css("color", "red")
                } else {
                    $("#textcount").css("color", "#ccc")
                }
                $("#textcount").text($scope.model.detail.length + "/" + $scope.textcount);

                if ($scope.model.detail.length > wordCount) {
                    $scope.model.detail = $scope.model.detail.substr(0, wordCount);
                }
            },
            search: function () {
                merchIntroduceService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    merchIntroduceService.get({ id: $stateParams.id }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '编辑';
                } else {
                    merchIntroduceService.get({ id: 0 }).success(function (response) {
                        $scope.model = response.data;
                    });
                    $scope.breadcrumb = '新增';
                }
            },
            save: function () {
                var model = $scope.model;
                $scope.psdisabled = true;
                if (model.id == 0) {
                    merchIntroduceService.add(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                } else {
                    merchIntroduceService.update(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                }
            },
            remove: function (id) {
                var modal = utils.confirm({ msg: "是否需要删除该商家", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    merchIntroduceService.delete(id).success(function () {
                        methods.search();
                    });
                })
            },
            updateStatus: function (id, status) {
                var tip = "停用";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该商家", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    merchIntroduceService.updateStatus(id, status).success(function () {
                        methods.search();
                    });
                })
            },
            updateIsRecommend: function (id, isRecommend) {
                var tip = isRecommend == 1 ? "推荐" : "下架";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该商家", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    merchIntroduceService.updateIsRecommend(id, isRecommend).success(function () {
                        methods.search();
                    });
                })
            },
            goBack: function () {
                history.back();
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
        }

        angular.extend($scope, methods);
        if ($state.includes('app.merchIntroduce.detail')) {
            methods.edit();
        } else {
        	methods.search();
    	}
    });
});
